<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>可自定义抽屉组件</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Arial', sans-serif;
            background-color: #f5f5f5;
            padding: 20px;
        }

        .container {
            max-width: 800px;
            margin: 0 auto;
        }

        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 30px;
        }

        /* 控制面板样式 */
        .control-panel {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }

        .control-group {
            margin-bottom: 15px;
        }

        label {
            display: inline-block;
            width: 120px;
            font-weight: bold;
            color: #555;
        }

        select, button {
            padding: 8px 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
        }

        button {
            background-color: #007bff;
            color: white;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #0056b3;
        }

        /* 抽屉遮罩层 */
        .drawer-overlay {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            display: none;
            z-index: 1000;
        }

        /* 抽屉容器 */
        .drawer {
            position: fixed;
            background: white;
            box-shadow: 0 4px 20px rgba(0,0,0,0.15);
            z-index: 1001;
            /* 过渡动画效果 */
            transition: transform 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
        }

        /* 顶部抽屉 */
        .drawer-top {
            top: 0;
            left: 0;
            width: 100%;
            height: 300px;
            transform: translateY(-100%);
        }

        .drawer-top.open {
            transform: translateY(0);
        }

        /* 底部抽屉 */
        .drawer-bottom {
            bottom: 0;
            left: 0;
            width: 100%;
            height: 300px;
            transform: translateY(100%);
        }

        .drawer-bottom.open {
            transform: translateY(0);
        }

        /* 左侧抽屉 */
        .drawer-left {
            top: 0;
            left: 0;
            width: 300px;
            height: 100%;
            transform: translateX(-100%);
        }

        .drawer-left.open {
            transform: translateX(0);
        }

        /* 右侧抽屉 */
        .drawer-right {
            top: 0;
            right: 0;
            width: 300px;
            height: 100%;
            transform: translateX(100%);
        }

        .drawer-right.open {
            transform: translateX(0);
        }

        /* 抽屉内容 */
        .drawer-header {
            padding: 15px 20px;
            border-bottom: 1px solid #eee;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .drawer-title {
            font-size: 18px;
            font-weight: bold;
            color: #333;
        }

        .drawer-close {
            background: none;
            border: none;
            font-size: 20px;
            cursor: pointer;
            color: #999;
            /* 添加旋转过渡动画 */
            transition: transform 0.3s ease;
        }

        /* 鼠标悬浮时旋转45度 */
        .drawer-close:hover {
            transform: rotate(90deg);
            color: #333;
        }

        .drawer-content {
            padding: 20px;
            height: calc(100% - 51px);
            overflow-y: auto;
        }

        .drawer-content p {
            line-height: 1.6;
            color: #666;
            margin-bottom: 15px;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>可自定义抽屉组件</h1>

    <!-- 控制面板 -->
    <div class="control-panel">
        <div class="control-group">
            <label for="position">抽屉方向：</label>
            <select id="position">
                <option value="left">左侧</option>
                <option value="right">右侧</option>
                <option value="top">顶部</option>
                <option value="bottom">底部</option>
            </select>
        </div>

        <div class="control-group">
            <label for="animation">动画效果：</label>
            <select id="animation">
                <option value="ease">缓入缓出</option>
                <option value="linear">线性</option>
                <option value="ease-in">缓入</option>
                <option value="ease-out">缓出</option>
            </select>
        </div>

        <button id="openDrawer">打开抽屉</button>
    </div>

    <!-- 抽屉遮罩层 -->
    <div class="drawer-overlay" id="drawerOverlay"></div>

    <!-- 抽屉容器 -->
    <div class="drawer" id="drawer">
        <div class="drawer-header">
            <div class="drawer-title">抽屉标题</div>
            <button class="drawer-close" id="closeDrawer">×</button>
        </div>
        <div class="drawer-content">
            <p>这是一个可自定义方向和动画效果的抽屉组件。</p>
            <p>您可以选择抽屉从四个方向弹出：</p>
            <ul>
                <li>从左侧滑入</li>
                <li>从右侧滑入</li>
                <li>从顶部滑入</li>
                <li>从底部滑入</li>
            </ul>
            <p>同时支持多种过渡动画效果，让用户体验更加流畅自然。</p>
            <p>抽屉组件在现代网页设计中非常常用，特别适合用于移动端或需要节省页面空间的场景。</p>
            <p>通过CSS3的transition属性和transform变换，我们可以实现平滑的动画效果。</p>
            <p>关闭按钮在鼠标悬浮时会有旋转的过渡动画效果。</p>
        </div>
    </div>
</div>

<script>
    // 获取DOM元素
    const drawer = document.getElementById('drawer');
    const drawerOverlay = document.getElementById('drawerOverlay');
    const openBtn = document.getElementById('openDrawer');
    const closeBtn = document.getElementById('closeDrawer');
    const positionSelect = document.getElementById('position');
    const animationSelect = document.getElementById('animation');

    // 初始化抽屉状态
    let currentPosition = 'left';
    let currentAnimation = 'ease';

    // 更新抽屉方向
    function updateDrawerPosition() {
        // 移除所有方向类
        drawer.classList.remove('drawer-left', 'drawer-right', 'drawer-top', 'drawer-bottom');

        // 添加当前选择的方向类
        drawer.classList.add(`drawer-${currentPosition}`);
    }

    // 更新动画效果
    function updateAnimation() {
        // 设置过渡动画的时间函数
        drawer.style.transitionTimingFunction = currentAnimation;
    }

    // 打开抽屉
    function openDrawer() {
        // 更新抽屉方向和动画
        updateDrawerPosition();
        updateAnimation();

        // 显示遮罩层
        drawerOverlay.style.display = 'block';

        // 触发重排，确保动画正常执行
        drawer.offsetHeight;

        // 添加打开状态类
        drawer.classList.add('open');

        // 防止页面滚动
        document.body.style.overflow = 'hidden';
    }

    // 关闭抽屉
    function closeDrawer() {
        // 移除打开状态类
        drawer.classList.remove('open');

        // 隐藏遮罩层
        drawerOverlay.style.display = 'none';

        // 恢复页面滚动
        document.body.style.overflow = 'auto';
    }

    // 监听方向选择变化
    positionSelect.addEventListener('change', function() {
        currentPosition = this.value;
    });

    // 监听动画选择变化
    animationSelect.addEventListener('change', function() {
        currentAnimation = this.value;
    });

    // 绑定打开按钮事件
    openBtn.addEventListener('click', openDrawer);

    // 绑定关闭按钮事件
    closeBtn.addEventListener('click', closeDrawer);

    // 点击遮罩层关闭抽屉
    drawerOverlay.addEventListener('click', closeDrawer);

    // 初始化抽屉
    updateDrawerPosition();
    updateAnimation();
</script>
</body>
</html>